#!/bin/bash

# 邮箱配置功能测试脚本

echo "=========================================="
echo "  📧 邮箱配置功能测试"
echo "=========================================="
echo ""

# 获取Token
TOKEN="0a5ad3db5ef69f6353f418e1c6c0a0ee9bae199d"
API_BASE="http://localhost:8000/api"

echo "🔍 测试1: 获取当前用户的邮箱配置"
echo "----------------------------------------"
curl -s -X GET "${API_BASE}/email-configs/my_config/" \
  -H "Authorization: Token ${TOKEN}" \
  -H "Content-Type: application/json" | jq '.' || echo "未配置邮箱（正常）"
echo ""
echo ""

echo "🔍 测试2: 创建邮箱配置（需要真实的邮箱信息）"
echo "----------------------------------------"
echo "提示: 此测试需要您提供真实的163邮箱和授权码"
echo "如果您还没有准备好，可以跳过此测试"
echo ""
read -p "是否创建测试配置? (y/N): " create_config

if [[ "$create_config" == "y" || "$create_config" == "Y" ]]; then
    read -p "请输入163邮箱地址: " email_address
    read -p "请输入授权码: " auth_code
    
    echo ""
    echo "正在创建配置..."
    curl -s -X POST "${API_BASE}/email-configs/" \
      -H "Authorization: Token ${TOKEN}" \
      -H "Content-Type: application/json" \
      -d "{
        \"email_provider\": \"163\",
        \"email_address\": \"${email_address}\",
        \"smtp_server\": \"smtp.163.com\",
        \"smtp_port\": 465,
        \"use_ssl\": true,
        \"authorization_code\": \"${auth_code}\",
        \"is_active\": true
      }" | jq '.'
    echo ""
else
    echo "跳过配置创建测试"
fi
echo ""

echo "🔍 测试3: 查询所有邮箱配置"
echo "----------------------------------------"
curl -s -X GET "${API_BASE}/email-configs/" \
  -H "Authorization: Token ${TOKEN}" \
  -H "Content-Type: application/json" | jq '.'
echo ""
echo ""

echo "=========================================="
echo "  ✅ 测试完成"
echo "=========================================="
echo ""
echo "📝 接下来的步骤："
echo "1. 访问前端: http://localhost:8081/email-config"
echo "2. 配置您的163邮箱"
echo "3. 测试连接"
echo "4. 保存配置"
echo "5. 返回仪表盘执行巡检并发送邮件"
echo ""
echo "📚 查看详细文档："
echo "- EMAIL_CONFIG_QUICKSTART.md (快速开始)"
echo "- EMAIL_CONFIG_FEATURE.md (完整功能说明)"
echo ""
